﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Hotel
{
    public partial class Profiles : Form
    {
        public Profiles()
        {
            InitializeComponent();
        }

        public static string UserName = string.Empty;

        CNDatabase dt = new CNDatabase();
                
        private void PgSetting_Load(object sender, EventArgs e)
        {
            //Session of Staff
            if (!string.IsNullOrEmpty(UserName))
                LbSESSION.Text = "Work Session :  " + UserName;
            LbStaff.Text = UserName;
            
            loadInfo();
            labelRole();
        }

        
                //----------------------------------------Method----------------------------------------//

        //Load Info of Staff from Database to Textbox
        private void loadInfo()
        {
            this.TbName.Text = dt.getOneData("SELECT [Fullname] FROM Account where IDAccount = '" + UserName + "'").ToString();
            this.DatePickBirth.Text = dt.getOneData("SELECT [Birthday] FROM Account where IDAccount = '" + UserName + "'").ToString();
            this.TbAddress.Text = dt.getOneData("SELECT [Address] FROM Account where IDAccount = '" + UserName + "'").ToString();
            this.TbPhone.Text = dt.getOneData("SELECT [Phone] FROM Account where IDAccount = '" + UserName + "'").ToString();
            this.TbMail.Text = dt.getOneData("SELECT [Email] FROM Account where IDAccount = '" + UserName + "'").ToString();
        }

        //Set Inactive for TextBox of Information
        private void setReadOnlyText()
        {
            GbChangeInfo.Visible = false;

            TbName.ReadOnly = true;
            TbAddress.ReadOnly = true;
            TbPhone.ReadOnly = true;
            TbMail.ReadOnly = true;
        }

        //Set Active for TextBox of Information
        private void setWriteText()
        {
            GbChangeInfo.Visible = true;
            GbChangeInfo.BringToFront();

            TbName.ReadOnly = false;
            TbAddress.ReadOnly = false;
            TbPhone.ReadOnly = false;
            TbMail.ReadOnly = false;
        }

        //Load Label TypeRole
        private void labelRole()
        {
            string role = dt.getOneData("SELECT [IDRole] FROM Account where IDAccount = '" + UserName + "'").ToString();
            if (role == "1")
            {
                LbRole.Text = " (Role : Admin)";
            }
            else
            {
                LbRole.Text = " (Role : Staff)";
            }
        }

                //----------------------------------------Function----------------------------------------//

        //Show GroupBox change PW
        private void BtChangePW_Click(object sender, EventArgs e)
        {
            GbChangePW.Visible = true;
            GbInfo.Visible = false;
        }

        //Button Comfirm change PW
        private void BtComfirmPW_Click(object sender, EventArgs e)
        {
            try
            {
                //Check old password
                dt.connect();
                string qrcheckPW = "select Password from Account where IDAccount ='" + UserName + "' and Password = '" + TbPresentCode.Text + "'";
                SqlCommand cmCheck = new SqlCommand(qrcheckPW, dt.cn);
                object a = cmCheck.ExecuteScalar();
                dt.cn.Close();
                if (a == null)
                {
                    MessageBox.Show(" The present code you entered is incorrect !");
                    TbNewCode.Text = "";
                    TbRetypeCode.Text = "";
                    TbPresentCode.Text = "";
                }
                else
                {
                    //Error : when 2 new PW do not match, will occur error 
                    if ((TbRetypeCode.Text != TbNewCode.Text) || (TbRetypeCode.Text.Length < 5) || (TbNewCode.Text.Length < 5))
                    {
                        MessageBox.Show("The new access code do not match !");
                        TbNewCode.Text = "";
                        TbRetypeCode.Text = "";
                        TbPresentCode.Text = "";
                    }
                    else //Accept change PW
                    {
                        dt.connect();
                        string qrchangePW = "update Account set Password = '" + TbRetypeCode.Text + "'";
                        SqlCommand cm = new SqlCommand(qrchangePW, dt.cn);
                        cm.ExecuteNonQuery();
                        dt.cn.Close();
                        MessageBox.Show("Change access code successfully !");
                        //Hide GroupBox change PW
                        GbChangePW.Visible = false;
                        GbInfo.Visible = true;
                    }
                }
            }
            catch (Exception) { }
        }
       
        //Button Cancel change PW
        private void PgSetBtCANCEL_Click(object sender, EventArgs e)
        {
            GbChangePW.Visible = false;
            GbInfo.Visible = true;
        }

        //Show GroupBox change Info
        private void BtChangeInfo_Click(object sender, EventArgs e)
        {
            setWriteText();
        }

        //Button Confirm change Info
        private void BtConfirmInfo_Click(object sender, EventArgs e)
        {
            try
            {
                dt.connect();
                string QrUpdateInfo = "UPDATE Account SET Fullname= N'" + TbName.Text + "', Birthday= convert(datetime,'" + DatePickBirth.Text + "', 103), Address= N'" + TbAddress.Text + "', Phone='" + TbPhone.Text + "', Email='" + TbMail.Text + "'  WHERE IDAccount = '" + UserName + "'";
                SqlCommand cm = new SqlCommand(QrUpdateInfo, dt.cn);
                cm.ExecuteNonQuery();
                dt.cn.Close();
                MessageBox.Show("Update Information successfully !");
                setReadOnlyText();
            }
            catch (Exception)
            {
                MessageBox.Show("Error ! Update failure ");
                dt.cn.Close();
            }
        }

        //Button Cancel change Info
        private void BtCancelInfo_Click(object sender, EventArgs e)
        {
            setReadOnlyText();
            loadInfo();           
        }

        //Show MessBox PERMISSION TO ACCESS
        private void BtAdvanced_Click(object sender, EventArgs e)
        {
            string RoleAD = dt.getOneData("SELECT [IDRole] FROM Account where IDAccount = '" + UserName + "'").ToString();
            if (RoleAD != "1")
                MessageBox.Show("You doesn't have permission to access this function!", "Access Denied....");
            else
            {
                Account ac = new Account();
                ac.Show();
            }
        }

        

        
      
        
        }

    }